home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / USoundPlayer (PP) / USoundPlayer.sit / USoundPlayer / USoundPlayer.cp < prev    next >
Text File  |  1995-08-28  |  5KB  |  210 lines

  1. // ===========================================================================
  2. //    USoundPlayer.cp                    ⌐ 1995, âric Forget. All rights reserved.
  3. // ===========================================================================
  4. //    
  5. //    ************************************************************************
  6. //    *                                                                      *
  7. //    *    Before using this code you should read the "License Agreement"     *
  8. //    *    document and agree with it.                                        *
  9. //    *                                                                      *
  10. //    ************************************************************************
  11. //
  12. //    USoundPlayer is a wrapper class to make life easier when playing a
  13. //    sound handle. You can play a sound by name, resource ID or SndListHandle.
  14. //
  15. // ---------------------------------------------------------------------------
  16. //
  17. //    Instruction Notes:
  18. //    -----------------
  19. //
  20. //    Call one of the USoundPlayer::PlaySound() method. That's all!
  21. //
  22. // ---------------------------------------------------------------------------
  23.  
  24. #include    "USoundPlayer.h"
  25.  
  26.  
  27.  
  28. // ---------------------------------------------------------------------------
  29. //        Ñ Static member
  30. // ---------------------------------------------------------------------------
  31.  
  32. USoundPlayer    *USoundPlayer::sSoundPlayer = nil;
  33. SndListHandle    USoundPlayer::sSoundHandle = nil;
  34. SndChannelPtr    USoundPlayer::sSoundChannel = nil;
  35.  
  36.  
  37.  
  38. // ---------------------------------------------------------------------------
  39. //        Ñ Initialize()
  40. // ---------------------------------------------------------------------------
  41.  
  42. void
  43. USoundPlayer::Initialize()
  44. {
  45.     if(sSoundPlayer == nil) {
  46.     
  47.         sSoundPlayer = new USoundPlayer;
  48.     }
  49. }
  50.  
  51.  
  52. // ---------------------------------------------------------------------------
  53. //        Ñ Dispose
  54. // ---------------------------------------------------------------------------
  55.  
  56. void
  57. USoundPlayer::Dispose()
  58. {
  59.     if(sSoundPlayer != nil) {
  60.     
  61.         delete sSoundPlayer;
  62.         sSoundPlayer = nil;
  63.     }
  64. }
  65.  
  66.  
  67. // ---------------------------------------------------------------------------
  68. //        Ñ PlaySound
  69. // ---------------------------------------------------------------------------
  70.  
  71. void
  72. USoundPlayer::PlaySound(
  73.     ConstStr255Param    inSoundName)
  74. {
  75.     SndListHandle    theSoundH = (SndListHandle)::GetNamedResource('snd ', inSoundName);
  76.     
  77.     
  78.     if(theSoundH) {
  79.     
  80.         PlaySoundHandle(theSoundH);
  81.     }
  82. }
  83.  
  84.  
  85. // ---------------------------------------------------------------------------
  86. //        Ñ PlaySound
  87. // ---------------------------------------------------------------------------
  88.  
  89. void
  90. USoundPlayer::PlaySound(
  91.     ResIDT        inSoundID)
  92. {
  93.     SndListHandle    theSoundH = (SndListHandle)::GetResource('snd ', inSoundID);
  94.     
  95.     
  96.     if(theSoundH) {
  97.     
  98.         PlaySoundHandle(theSoundH);
  99.     }
  100. }
  101.  
  102.  
  103. // ---------------------------------------------------------------------------
  104. //        Ñ PlaySound
  105. // ---------------------------------------------------------------------------
  106.  
  107. void
  108. USoundPlayer::PlaySound(
  109.     SndListHandle    inSoundHandle)
  110. {
  111.     PlaySoundHandle(inSoundHandle);
  112. }
  113.  
  114.  
  115. // ---------------------------------------------------------------------------
  116. //        Ñ SpendTime
  117. // ---------------------------------------------------------------------------
  118.  
  119. void
  120. USoundPlayer::SpendTime(
  121.     const EventRecord & /*inMacEvent*/)
  122. {
  123.     SCStatus    theStatus;
  124.     OSErr        err;
  125.     
  126.     
  127.     err = ::SndChannelStatus(sSoundChannel, sizeof(SCStatus), &theStatus);
  128.     
  129.     
  130.     if ((err != noErr) || (!theStatus.scChannelBusy)) {
  131.         
  132.         StopCurrentSound();
  133.     }
  134. }
  135.     
  136.  
  137. // ---------------------------------------------------------------------------
  138. //        Ñ PlaySoundHandle
  139. // ---------------------------------------------------------------------------
  140.  
  141. void
  142. USoundPlayer::PlaySoundHandle(
  143.     SndListHandle    inSoundHandle)
  144. {
  145.     OSErr        err = noErr;
  146.  
  147.     
  148.     Initialize();
  149.     StopCurrentSound();
  150.     
  151.     sSoundHandle = inSoundHandle;
  152.     err = ::HandToHand((Handle *)&(sSoundHandle));
  153.     
  154.     if (!err) {
  155.         
  156.         if (!sSoundChannel) {
  157.         
  158.             sSoundChannel = (SndChannelPtr) ::NewPtrClear(sizeof(SndChannel));
  159.             
  160.             if (sSoundChannel != nil) {
  161.                 
  162.                 sSoundChannel->qLength = stdQLength;
  163.                 err = ::SndNewChannel(&sSoundChannel, sampledSynth, 0, nil);
  164.                 
  165.                 if (err) {
  166.                     
  167.                     ::DisposePtr((Ptr)sSoundChannel);
  168.                     sSoundChannel = nil;
  169.                 }
  170.             }
  171.         }
  172.     
  173.         if (sSoundHandle && sSoundChannel) {
  174.         
  175.             ::HLockHi((Handle)sSoundHandle);
  176.             err = ::SndPlay(sSoundChannel, sSoundHandle,true);
  177.         }
  178.         if (err) {
  179.             
  180.             StopCurrentSound();
  181.         
  182.         } else {
  183.             
  184.             sSoundPlayer->StartIdling();
  185.         }
  186.     }
  187. }
  188.  
  189.  
  190. // ---------------------------------------------------------------------------
  191. //        Ñ StopCurrentSound
  192. // ---------------------------------------------------------------------------
  193.  
  194. void
  195. USoundPlayer::StopCurrentSound()
  196. {
  197.     if (sSoundChannel) {
  198.     
  199.         ::SndDisposeChannel(sSoundChannel, true);
  200.         sSoundChannel = nil;
  201.     }
  202.     
  203.     if (sSoundHandle) {
  204.     
  205.         ::DisposeHandle((Handle)sSoundHandle);
  206.         sSoundHandle = nil;
  207.     }
  208.     
  209.     sSoundPlayer->StopIdling();
  210. }